<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/25
 * Time: 13:36
 */

namespace app\adminapiv3\service\asset_manage\ministry;


use app\adminapiv3\service\BaseService;
use app\common\service\asset_manage\ministry\HostManager;
use app\common\service\asset_manage\ministry\HostMinistry;

class ManagerService extends BaseService
{
    /**
     * @name 判断是否存在
     * @param $condition
     * @return bool
     * @author wx
     * @date 2019/10/25 13:54
     */
    public function checkExist($condition)
    {
        $server = new HostManager();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }

    /**
     * @name 列表
     * @param $params
     * @return array
     * @author wx
     * @date 2019/10/25 14:08
     */
    public function list($params)
    {
        $server = new HostManager();
        $list = $server->list($params);

        return $list;
    }

    /**
     * @name 添加
     * @param $addData
     * @return bool
     * @author wx
     * @date 2019/10/25 14:25
     */
    public function add($addData)
    {
        global $logArray;
        $logArray['content'] = '添加责任人';

        //1. 检查部门是否存在
        $ministryServer = new HostMinistry();
        $ministryInfo = $ministryServer->findData(['id' => $addData['ministry_id']]);
        if (!$ministryInfo) {
            $this->setError($this->CODE_FAIL, '该部门不存在');
            return false;
        }

        //2. 检出重名
        if ($this->checkExist(['name' => $addData['name'], 'phone' => $addData['phone']])) {
            $this->setError($this->CODE_FAIL, '请勿重复添加责任人');
        }

        //3. 添加
        $addData['ctime'] = time();
        $server = new HostManager();
        $server->add($addData);

        $model = new \app\common\model\HostManager();

        $logArray['new_info'] = '姓名：'.$addData['name'].'；性别：'.$model->genderArr[$addData['gender']].
            '；部门：'.$ministryInfo['name'].'；联系电话：'.$addData['phone'];


        return true;
    }

    /**
     * @name 修改
     * @param $id
     * @param $updateData
     * @return bool
     * @author wx
     * @date 2019/10/25 14:25
     */
    public function modify($id, $updateData)
    {
        global $logArray;
        $logArray['content'] = '修改责任人';

        //1. 检查部门是否存在
        $ministryServer = new HostMinistry();
        $ministryInfo = $ministryServer->findData(['id' => $updateData['ministry_id']]);
        if (!$ministryInfo) {
            $this->setError($this->CODE_FAIL, '该部门不存在');
            return false;
        }

        //2. 修改
        $server = new HostManager();
        $info = $server->findData(['id' => $id]);

        if (!$info) {
            $this->setError($this->CODE_FAIL, '该责任人不存在');
            return false;
        }

        if($info['ministry_id'] != $updateData['ministry_id']){
            $ministryOldInfo = $ministryServer->findData(['id' => $info['ministry_id']]);
            $oldInfoName = isset($ministryOldInfo['name']) ? $ministryOldInfo['name'] : '';
        }else{
            $oldInfoName = $ministryInfo['name'];
        }


        $server->modify(['id' => $id], $updateData);

        $model = new \app\common\model\HostManager();

        $logArray['old_info'] = '姓名：'.$info['name'].'；性别：'.$model->genderArr[$info['gender']].
            '；部门：'.$oldInfoName.'；联系电话：'.$info['phone'];
        $logArray['new_info'] = '姓名：'.$updateData['name'].'；性别：'.$model->genderArr[$updateData['gender']].
            '；部门：'.$ministryInfo['name'].'；联系电话：'.$updateData['phone'];

        return true;
    }

    /**
     * @name 删除
     * @param $id
     * @return bool
     * @author wx
     * @date 2019/10/25 14:24
     */
    public function del($id)
    {
        global $logArray;
        $logArray['content'] = '删除责任人';

        $server = new HostManager();

        $info = $server->findData(['id' => $id]);

        if (!$info) {
            $this->setError($this->CODE_FAIL, '该责任人不存在');
            return false;
        }

        $ministryServer = new HostMinistry();
        $ministryOldInfo = $ministryServer->findData(['id' => $info['ministry_id']]);
        $oldInfoName = isset($ministryOldInfo['name']) ? $ministryOldInfo['name'] : '';

        $condition[] = array('id', 'in', $id);
        $server->del($condition);

        $model = new \app\common\model\HostManager();
        $logArray['old_info'] = '姓名：'.$info['name'].'；性别：'.$model->genderArr[$info['gender']].
            '；部门：'.$oldInfoName.'；联系电话：'.$info['phone'];

        return true;
    }

}